<?php
// 本文档自动生成，仅供测试运行
class IndexAction extends Action
{
	public function _initialize()
	{
		$config = D('setting')->getConfig();
		$categoryItem = getCategoryCache();
		$this->assign('categoryItem',$categoryItem);
		$this->assign('site_config',$config);
		A('Public')->right();//载入右侧信息
		
	}
    /**
    +----------------------------------------------------------
    * 首页
    +----------------------------------------------------------
    */
    public function index()
    {
    	$IndexData = array();
    	$IndexData['topArticle'] = self::getTopArticle();//获取置顶文章,经过缓存处理
    	$posts = D('posts');
    	$count = $posts->where(array('is_top'=>'0'))->count();
    	import("ORG.Util.Page");
		$Page = new Page($count,9,1);
		$show = $Page->show();
		$IndexData['commonArticle'] = $posts->where(array('is_top'=>'0'))->field('id,subject,description,click_count,createdate')->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
//		$IndexData['flink'] = self::getIndexFlink();//首页友情链接,经过缓存处理
		$this->assign('page',$show);
		$this->assign('IndexData',$IndexData);
		$this->display();
    	
    	
    }
    
    //tag标签
    public function showtag()
    {
    	//过滤
    	$tagName = $_GET['tagname'] ? trim($_GET['tagname']) : '';
    	if (empty($tagName))$this->error('tag名字不能为空!');
    	
    	$taginfo = D('tag')->getTagByName($tagName);
    	
    	$tagaid = rtrim($taginfo['aid'],",");
    	
    	if ($tagaid)
    	{
	    	import("ORG.Util.Page");
	    	$posts = D('posts');
	    	$count = count(explode(',', $tagaid));
	    	$Page = new Page($count,10); 
			$show = $Page->show();
	    	$tags = $posts->where('id in ('.$tagaid.')')->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->field('id,subject,description,createdate,click_count')->select();
	    	$this->assign('page',$show);
	    	$this->assign('taglist',$tags);
			
	    	
    	}
    	else
    	{
    		$this->assign('errorMsg','无此Tag信息!!');
    	}
    	
    	$this->display();
    }
    
    //搜索模块
	public function search()
	{
		$wd = $_GET['wd'] ? trim($_GET['wd']) : '';
		if (empty($wd))$this->error('搜索关键词不能为空!');
		$posts = D('posts');
		$count = $posts->where("subject like '%$wd%' ")->count();
		if ($count>'0')
		{
			import("ORG.Util.Page");
			$Page = new Page($count,10); 
			$show = $Page->show();
			$result = $posts->where("subject like '%$wd%' ")->order('id desc')->field('id,subject,description,createdate,click_count')->limit($Page->firstRow.','.$Page->listRows)->select();
			$this->assign('searchData',$result);
			$this->assign('page',$show);
			
			
		}
		else
		{
			$this->assign('errorMsg','抱歉，我们沒有准备您需要的文章!!');
			
		}
		$this->assign('wd',$wd);
		$this->display();
		
		
	}
	
	private static function getTopArticle()
	{
		if (!F('getTopArticle'))
		{
			$limit = '0,5';
			$tempdata = M('posts')->where(array('is_top'=>'1'))->field('id,subject,createdate,click_count')->order('id desc')->limit($limit)->select();
			F('getTopArticle',$tempdata);
		}
		return F('getTopArticle');
	}
	
	private static function getIndexFlink()
	{
		if (!F('getIndexFlink'))
		{
			$limit = '0,24';
			$tempdata = M('flink')->limit($limit)->order('flink_approve_time desc')->where('flink_status = 1')->select();
			F('getIndexFlink',$tempdata);
		}
		return F('getIndexFlink');
	}
	
	//友情链接
	public function links()
	{
		$linkData = M('flink')->where('flink_status = 1')->order('flink_approve_time desc')->select();
		$this->assign('flink',$linkData);
		$this->display();
		
	}
	
	//Feed
	public function feed()
	{
		header('Content-type: application/xml');
		$cid = isset($_GET['cid']) ? intval($_GET['cid']) : '';

		$where = $cid ? "and cid = $cid" : '';

		$blog = M('posts')->field('id,subject,description,createdate')->where("1 = 1".$where)->order('id desc')->select();
		echo '<?xml version="1.0" encoding="utf-8"?>
		<rss version="2.0">
		<channel>
		<title><![CDATA[YunPHP - 因为相信,所以坚持 - 专注PHP开发]]></title> 
		<description><![CDATA[YunPHP是以PHP开发技术交流为主的博客,帮助开发者解决在实际项目中经常遇到的PHP问题]]></description>
		<link>http://www.yunphp.cn</link>
		<language>zh-cn</language>
		<generator>www.yunphp.cn</generator>';
		foreach ($blog as $v)
		{
			echo "
			<item>
				<title>{$v['subject']}</title>
				<link>http://www.yunphp.cn/post/{$v['id']}</link>
				<description><![CDATA[{$v['description']}]]></description>
				<author>米修</author>
				<pubDate>{$v['createdate']}</pubDate>
			
			</item>";
		}
		echo "</channel>";
		echo "</rss>";
	
	}
	
	//投稿
	public function tougao()
	{
		$tougao = array();
		$tougao['commentcount'] = M('comment')->where(array('comment_approved'=>'1','comment_type'=>'tougao'))->count();
		$tougao['comment'] = M('comment')->where(array('comment_approved'=>'1','comment_type'=>'tougao'))->order('comment_ID desc')->select();
		$this->assign('tougao',$tougao);
		$this->display();
	}
	
	//URL地址跳转
	public function r()
	{
		$url = !isset($_GET['url']) ? trim($_GET['url']) : 'http://www.yunphp.cn';
		redirect($url);
	}
}
?>